#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/bitops.h>
#include <linux/interrupt.h>
#include <linux/bug.h>			/* For BUG_ON.  */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <asm-generic/bitops/non-atomic.h>
#include <asm-generic/bitops/find.h>
#include <linux/bitmap.h>


/*
 * The available bitmap operations and their rough meaning in the
 * case that the bitmap is a single unsigned long are thus:
 *
 * Note that nbits should be always a compile time evaluable constant.
 * Otherwise many inlines will generate horrible code.
 *
 * bitmap_zero(dst, nbits)			*dst = 0UL
 * bitmap_fill(dst, nbits)			*dst = ~0UL
 * bitmap_copy(dst, src, nbits)			*dst = *src
 * bitmap_and(dst, src1, src2, nbits)		*dst = *src1 & *src2
 * bitmap_or(dst, src1, src2, nbits)		*dst = *src1 | *src2
 * bitmap_xor(dst, src1, src2, nbits)		*dst = *src1 ^ *src2
 * bitmap_andnot(dst, src1, src2, nbits)	*dst = *src1 & ~(*src2)
 * bitmap_complement(dst, src, nbits)		*dst = ~(*src)
 * bitmap_equal(src1, src2, nbits)		Are *src1 and *src2 equal?
 * bitmap_intersects(src1, src2, nbits) 	Do *src1 and *src2 overlap?
 * bitmap_subset(src1, src2, nbits)		Is *src1 a subset of *src2?
 * bitmap_empty(src, nbits)			Are all bits zero in *src?
 * bitmap_full(src, nbits)			Are all bits set in *src?
 * bitmap_weight(src, nbits)			Hamming Weight: number set bits
 * bitmap_set(dst, pos, nbits)			Set specified bit area
 * bitmap_clear(dst, pos, nbits)		Clear specified bit area
 * bitmap_find_next_zero_area(buf, len, pos, n, mask)	Find bit free area
 * bitmap_find_next_zero_area_off(buf, len, pos, n, mask)	as above
 * bitmap_shift_right(dst, src, n, nbits)	*dst = *src >> n
 * bitmap_shift_left(dst, src, n, nbits)	*dst = *src << n
 * bitmap_remap(dst, src, old, new, nbits)	*dst = map(old, new)(src)
 * bitmap_bitremap(oldbit, old, new, nbits)	newbit = map(old, new)(oldbit)
 * bitmap_onto(dst, orig, relmap, nbits)	*dst = orig relative to relmap
 * bitmap_fold(dst, orig, sz, nbits)		dst bits = orig bits mod sz
 * bitmap_parse(buf, buflen, dst, nbits)	Parse bitmap dst from kernel buf
 * bitmap_parse_user(ubuf, ulen, dst, nbits)	Parse bitmap dst from user buf
 * bitmap_parselist(buf, dst, nbits)		Parse bitmap dst from kernel buf
 * bitmap_parselist_user(buf, dst, nbits)	Parse bitmap dst from user buf
 * bitmap_find_free_region(bitmap, bits, order)	Find and allocate bit region
 * bitmap_release_region(bitmap, pos, order)	Free specified bit region
 * bitmap_allocate_region(bitmap, pos, order)	Allocate specified bit region
 */

/*
 * Also the following operations in asm/bitops.h apply to bitmaps.
 *
 * set_bit(bit, addr)			*addr |= bit
 * clear_bit(bit, addr)			*addr &= ~bit
 * change_bit(bit, addr)		*addr ^= bit
 * test_bit(bit, addr)			Is bit set in *addr?
 * test_and_set_bit(bit, addr)		Set bit and return old value
 * test_and_clear_bit(bit, addr)	Clear bit and return old value
 * test_and_change_bit(bit, addr)	Change bit and return old value
 * find_first_zero_bit(addr, nbits)	Position first zero bit in *addr
 * find_first_bit(addr, nbits)		Position first set bit in *addr
 * find_next_zero_bit(addr, nbits, bit)	Position next zero bit in *addr >= bit
 * find_next_bit(addr, nbits, bit)	Position next set bit in *addr >= bit
 *
 */


#define  TRUE(arg) BUG_ON(!(arg));

#define MY_MAX_MINORS  1
#define   DEVICE_NAME  "device name 500"

DEFINE_KFIFO(myfifo, char, 1024);

struct my_device_data {
	unsigned char *buf;
	struct cdev cdev;
};

DECLARE_WAIT_QUEUE_HEAD(wq);

static struct my_device_data my_dev_data;

static int my_open(struct inode *inode, struct file *file) {
	int ret;
	pr_info("a3 my_open \n");
	ret = kfifo_put(&myfifo, 'e');
	ret = kfifo_put(&myfifo, 'n');
	ret = kfifo_put(&myfifo, 'd');
	ret = kfifo_put(&myfifo, '\n');
	return 0;
}

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("write");
	ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
	if (ret != 0) {
		pr_err("kfifo_from_user error");
		return 0;
	}
	if (len <= 0)
		return 0;
	*offset += len;
	wake_up(&wq);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *user_buffer,
		size_t count, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("read");
	ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
	if (len <= 0)
		return 0;
	*offset += len;
	return len;
}

unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
	int mask = 0;
	pr_info("my_poll \n");
	poll_wait(flip, &wq, table);
	if (kfifo_is_empty(&myfifo)) {

	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

int my_close(struct inode *inode, struct file *flip) {
	pr_info("my_close \n");
	pr_info("my_dev_data.buf : %s\n", my_dev_data.buf);
	return 0;
}

int my_mmap(struct file *flip, struct vm_area_struct *vma) {
	if (remap_pfn_range(vma, vma->vm_start,
			virt_to_phys(my_dev_data.buf) >> PAGE_SHIFT,
			vma->vm_end - vma->vm_start, vma->vm_page_prot))
		return -EAGAIN;
	return 0;
}

static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
		my_open, .read = my_read, .write = my_write, .poll = my_poll, .release =
		my_close, .mmap = my_mmap,

};

static dev_t mydev;

void aa(unsigned long arg) {
	pr_info("timer expires\n");
}


DECLARE_BITMAP(my_bits,64);

static __init int my_init(void) {
	int err;
	unsigned long hh ;
	unsigned long  addr;
	unsigned long dst;
	pr_info("a3 init_module\n");err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	cdev_init(&my_dev_data.cdev, &my_fops);
	cdev_add(&my_dev_data.cdev, MKDEV(MAJOR(mydev), 0), 1);
	addr = __get_free_page(GFP_KERNEL);my_dev_data.buf =(unsigned char*)addr;
	memset(my_dev_data.buf,0,PAGE_SIZE);

	pr_info("my_bits : %lu\n",*my_bits);
	set_bit(0,my_bits);
	  TRUE(*my_bits==1);
	  set_bit(1,my_bits);

//
//	pr_info("my_bits : %lu\n",*my_bits);
//	set_bit(2,my_bits);
//	 TRUE(*my_bits==6);
//	pr_info("my_bits : %lu\n",*my_bits);
//	set_bit(0,my_bits);
//	 TRUE(*my_bits==7);
//	pr_info("my_bits : %lu\n",*my_bits);
//	clear_bit(2,my_bits);
//	pr_info("my_bits : %lu\n",*my_bits);
//	change_bit(2,my_bits);
//	pr_info("my_bits : %lu\n",*my_bits);
//	set_bit(3,my_bits);
//	  hh = find_first_zero_bit(my_bits,64);
//	  TRUE(hh==4);
//	pr_info("find_first_zero_bit : %lu\n",hh);

	  hh =0;
	  pr_info("my_bits : %lu\n",*my_bits);

	bitmap_shift_right(&dst,my_bits,1,64);
	bitmap_shift_left(&dst,my_bits,1,64);
	pr_info("bitmap_shift_right : %lu\n",dst);
	TRUE(dst==6);
	return 0;
}

static void __exit my_exit(void) {
		pr_info("a3 cleanup_module\n");
/* release devs[i] fields */
		cdev_del(&my_dev_data.cdev);
		unregister_chrdev_region(mydev, MY_MAX_MINORS);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

